package com.ytjj.quartz.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.utils.feishu.FeiShuUtil;
import com.ytjj.qmyx.users.client.AdminClient;
import com.ytjj.quartz.service.OrdersService;
import com.ytjj.quartz.service.ReturnGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 手动触发相关定时任务
 */
@RequestMapping("/manual")
@RestController
@Slf4j
public class ManualController {

    @Autowired
    private OrdersService ordersService;
    @Resource
    private ReturnGoodsService returnGoodsService;

    @Resource
    private AdminClient adminClient;

    @GetMapping("/sendFeishu")
    public String sendFeishu() {
        FeiShuUtil.sendMsg("test quartz");
        return "ok";
    }

    /**
     * 手动退单
     */
    @GetMapping("/uploadOrderEveryDay")
    public void uploadOrderEveryDay() {
        log.info("===手动推订单到供应链开始===");
        try {
            ordersService.uploadOrderToSupply();
        } catch (Exception e) {
            log.error("手动推单出错,{}",e);
        }
        log.info("===手动推送次订单到供应链结束");
    }


    /**
     * 同步供应链订单状态
     */
    @GetMapping("/checkSupplyChainOrder")
    public String checkSupplyChainOrder(){
        try {
            log.info("===== 开始检查供应链订单状态修改 =====");
            ordersService.checkSupplyChainOrder(null);
            log.info("===== 结束检查供应链订单状态修改 =====");
        } catch (Exception e) {
            e.printStackTrace();
            return "同步失败";
        }
        return "同步成功";
    }

    /**
     * 同步售后单状态
     */
    @GetMapping("/checkSupplyChainReturnGoods")
    public String checkSupplyChainReturnGoods(){
        try {
            log.info("===== 开始检查供应商审核状态修改 =====");
            returnGoodsService.checkSupplyChainReturnGoods();
            log.info("===== 结束检查供应商审核状态修改 =====");
        } catch (Exception e) {
            e.printStackTrace();
            return "同步失败";
        }
        return "同步成功";

    }

    /**
     * 修复订单状态
     */
    @GetMapping("/repairOrderStatus")
    public String repairOrderStatus(String childOrderNo){
        try {
            log.info("===== 开始手动修复订单状态 =====");
            returnGoodsService.verifyReturnGoodsOrders(childOrderNo);
            log.info("===== 结束手动修复订单状态 =====");
        } catch (Exception e) {
            e.printStackTrace();
            return "修复失败";
        }
        return "修复成功";

    }

    /**
     * 测试分钟内是否产生支付订单
     * @param minute
     */
    @GetMapping("/checkIsPayOrderCount")
    public String checkIsPayOrderCount(int minute){
        ordersService.checkIsPayOrderCount(minute);
        return "ok";
    }

    @GetMapping("/check")
    public String check() {
        return "ok";
    }

    /**
     * 查询订单支付状态
     */
    @GetMapping("/checkPayOrderStatus")
    public String checkPayOrderStatus(String orderNo){
        ordersService.updateAliPayAppOrdersStatus(orderNo);
        return "ok";
    }

    /**
     * 客服标记自动退款订单退款
     */
    @GetMapping("/orderReturnAuto")
    public String orderReturnAuto(){
        returnGoodsService.orderReturnAuto();
        return "ok";
    }

    /**
     * 待退款同步到原路返回页(客服标记的)
     * @return
     */
    @GetMapping("/syncReturnRegisterByMark")
    public String syncReturnRegisterByMark(){
        returnGoodsService.syncReturnRegisterByMark();
        return "ok";
    }
}
